package com.hxx.day8a.mapper.iml;

import com.hxx.day8a.common.HxxUtils;
import com.hxx.day8a.domain.Book;
import com.hxx.day8a.domain.Emp;
import com.hxx.day8a.mapper.BaseMapper;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

public class BaseMapperImpl <T> implements BaseMapper<T> {
    //数据库
    private String db = "d:/zmx/db/";
    //属性文件的对象
    private Properties properties = new Properties();


    @Override
    public void save(T t) throws Exception{
        //反射类型
        Class<?> aClass = t.getClass();
        //通过反射获取所有的get方法
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods) {
            String name = method.getName();
            if (name.contains("get")){
                //通过get方法获取数据添加到属性文件中
                String key = name.replace("get", "").toLowerCase();
                Object value = method.invoke(t);
                properties.setProperty(key,value.toString());
            }
        }
       //存储到属性文件中
        FileWriter fileWriter = new FileWriter(db+ HxxUtils.fileName());
        properties.store(fileWriter,"save");
    }

    @Override
    public List<T> findAll(Class<?> aClass) throws Exception {
        List<T> list = new ArrayList<>();
        //读取所有的文件
        File file = new File(db);
        File[] files = file.listFiles();
        for (File f : files) {
           //加载属性文件
            properties.load(new FileReader(f));
            //通过反射创建对象
            T t = (T)aClass.newInstance();
            //获取所有的key和value
            Set<Map.Entry<Object, Object>> entries = properties.entrySet();
            for (Map.Entry<Object, Object> entry : entries) {
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();
                //set方法
                String setMethod = "set"+key.substring(0,1).toUpperCase()+key.substring(1);
                //根据属性名称获取对应的set方法中的参数类型
                Field field = aClass.getDeclaredField(key);
                Class<?> type = field.getType();
                //通过反射获取set方法
                Method setMethod1 = aClass.getDeclaredMethod(setMethod,type);
                //通过set方法设置值
                //需要进行类型的转换(字符串除外)
                if (type.getSimpleName().equals("Long")){
                    setMethod1.invoke(t,Long.parseLong(value.trim()));
                }else if (type.getSimpleName().equals("Integer")){
                    setMethod1.invoke(t,Integer.parseInt(value.trim()));
                }else if (type.getSimpleName().equals("Double")){
                    setMethod1.invoke(t,Double.parseDouble(value.trim()));
                }else {
                    setMethod1.invoke(t,value.trim());
                }
            }
            //添加到集合中
            list.add(t);
        }
        return list;
    }


    public static void main(String[] args)throws Exception {
        BaseMapperImpl<Emp> baseMapperImpl = new BaseMapperImpl<>();
        List<Emp> emps = baseMapperImpl.findAll(Emp.class);
        for (Emp emp : emps) {
            System.out.println(emp);
        }
    }
}

































